<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: mahuan<mahuan@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo  短信模板原型
 * @since 2015-9-2
 */

class ServiceSmsTemplateModel extends BaseModel {
    
    /* 自动验证规则 */
    protected $_validate = array(
        array('sms_template_name', 'require', '模板名称必须填写', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('sms_template_content', 'require', '模板内容必须填写', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('sms_template_name', '1,20', '模板名称必须小于20个字', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
        array('sms_template_content', '1,60', '模板内容必须小于60个字', self::MUST_VALIDATE, 'length', self::MODEL_BOTH)
    );
    /* 自动完成规则 */
    protected $_auto = array(
    	array('create_time', 'time', self::MODEL_INSERT, 'function'),
    	array('update_time', 'time', self::MODEL_UPDATE, 'function')
    );
    
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     * @author mahuan<mahuan@hrbjjhh.com>
     */
    public function getPk(){
    	return 'sms_template_id';
    }

    /**
     * @todo 返回短信模板替换后的内容
     * @author mahuan<mahuan1768@126.com>
     * 
     */

    public function getSmsTempleteContent($sms_template_id,$customer_id,$activity_id){
        $sms_template_id = (int)$sms_template_id;
        if($sms_template_id == 0){
            $this->error = '短信模板id不能为空';
            return false;
        }
        
        if($customer_id == 0){
            $this->error = '用户id不能为空';
            return false;
        }
        
        if($activity_id == 0){
            $this->error = '活动id不能为空';
            return false;
        }
        
        $result = M('ServiceSmsTemplate')->where(array('sms_template_id'=>$sms_template_id))->field('sms_template_content')->find();
        if(empty($result)){
            $this->error = '短信模板为空';
            return false;
        }else{
            $content = $result['sms_template_content'];
        }
        $find = array();
        $replace = array();
        
        if(strpos($content,'[客户姓名]')!==false){
            $find[] = '[客户姓名]';
            $replace[] = getServiceCustomerName($customer_id);
        }
        
        if(strpos($content,'[活动名称]')!==false){
            $find[] = '[活动名称]';
            $replace[] = getActivityName($activity_id);
        
        }
        
        if(strpos($content,'[活动开始时间]')!==false){
            $start_time=D('ServiceActivity')->activityShow($activity_id,'start_time');
            if($start_time){
                $find[] = '[活动开始时间]';
                $replace[] = date('m月d日',$start_time['start_time']);
            }
        }
        
        if(strpos($content,'[坐席名]')!==false){
            $find[] = '[坐席名]';
            $replace[] = getUserName(UID);
        }
        
        if(strpos($content,'[坐席电话]')!==false){
            $find[] = '[坐席电话]';
            $replace[] = getMobilePhone(UID);
        }
        return str_replace($find,$replace,$content);
        
    }
    
    /**
     * @todo 发送短息
     * @author fzh
     * @since 2015-9-14
     */
    public function sendSms($sms_template_id,$customer_id,$activity_id){
        $phone=getCustomerName($customer_id);
        if($phone){
            $content = $this->getSmsTempleteContent($sms_template_id,$customer_id,$activity_id);
            D('Api/Sms','Service')->sendSms($phone,$content);
            return true;
        }else{
            $this->error='手机号码错误';
            return false;
        }
    }
    
    
    /**
     * @todo 短信模板列表
     * @param array $where 查询条件数组
     * @param string $field 查询字段
     * @param string $orderby 排序
     * @return false|array 商户列表数组
     * @author sap
     * @since 2015-9-11
     */
    public function getSmsTemplateList($where = array(),$field= '',$orderby='sms_template_id desc'){
    	return $this->where($where)->field($field)->order($orderby)->select();
    }
}
